package fr.inserm.u915.organifooyi.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity(name="individual")
@Table(name="individual")
@NamedQueries({
    @NamedQuery(name="individual.list.all",
            query="select I from individual as I ORDER BY I.integralis"),
    @NamedQuery(name="individual.find.by.integralis",
    	query="select I from individual as I where I.integralis = :integralis ")
	})
public class Individual
	extends AbstractUpdatableObject
	implements HasDataType
	{
	@Column(name="integralis",unique=true)
	private String integralis;
	
	@OneToMany(mappedBy="individual",fetch=FetchType.LAZY,cascade=CascadeType.PERSIST)
	private List<ReceptionTubes> receptionTubes=new ArrayList<ReceptionTubes>();
	
	
	
	@Override
	@Transient
	public DataType getDataType()
		{
		return DataType.individual;
		}
	
	public void setIntegralis(String integralis)
		{
		this.integralis = integralis;
		}
	
	public String getIntegralis()
		{
		return integralis;
		}
	
	public List<ReceptionTubes> getReceptionTubes()
		{
		return receptionTubes;
		}
	
	public void setReceptionTubes(List<ReceptionTubes> receptionTubes)
		{
		this.receptionTubes = receptionTubes;
		}
	
	@Transient
	public List<Extraction> getExtractions()
		{
		 List<Extraction> ret=new ArrayList<Extraction>();
		List<ReceptionTubes> L1=getReceptionTubes();
		if(L1==null) return ret;
		for(ReceptionTubes r:L1)
			{
			 List<Extraction> L2=r.getExtractions();
			 if(L2==null) continue;
			 ret.addAll(L2);
			}
		return ret;
		}
	
	
	
	@Override
	public String toString() {
		return integralis!=null?integralis:super.toString();
		}
	}
